package com.hgkj.cloud.base.scheme.table;

import com.hgkj.cloud.base.scheme.ClsAnswerRequest;
import com.hgkj.cloud.base.scheme.ClsAnswerResult;
import com.hgkj.cloud.base.scheme.ClsScore;

import java.util.Map;

/**
 * 七、艾森克个性测验(成人)（Epq）共88题
 */
public class Table7 extends AbstractTable implements TableCalculate{
    String code = "Epq";

    private String red_Asterisk = "<label style='color: Red'>*</label>";

    @Override
    public String getCode() {
        return code;
    }

    @Override
    public ClsAnswerResult calc(ClsAnswerRequest answer) {
        return GetEpqFactorScore(answer);
    }

    @Override
    public String[] report(Map map) {
        return GetEpqReport(map);
    }

    private static ClsAnswerResult GetEpqFactorScore(ClsAnswerRequest answer) {
        ClsAnswerResult result = new ClsAnswerResult();
        result.setMessage("");
        result.setType(answer.getType());
        result.setUuid(answer.getUuid());
        try {
            // 1正确题数
            int MAX_NUM = 88;
            // '1精神质(p)，2内外向(e)，3神经质(n)，4掩饰程度(l)
            int intI;
            String[] strAnswer_c = answer.getAnswer().split(g_sChar);
            if (strAnswer_c.length > 1) {
                for (intI = 1; intI < strAnswer_c.length; intI++) {
                    strAnswer_c[intI] = strAnswer_c[intI].trim();
                }
            }

            String[] sAnswer = strAnswer_c[0].toString().split(g_nChar);

            if (sAnswer.length != MAX_NUM) {
                result.setMessage("答案数量不符！");
                return result;
            }

            int i, j, k;
            // 共2组，第一组0,1；第二组1,0
            String[][] strNumberA = {
                    { "2,6,9,11,18,38,42,56,62,72,88", "21,29,45", "", "4,8,16,24,28,40,44,48,52,54,60,64,70,79,83" },
                    { "22,26,30,34,46,50,66,68,75,76,81,85", "1,5,10,13,14,17,25,33,37,41,49,53,55,61,65,71,80,84",
                            "3,7,12,15,19,23,27,31,35,39,43,47,51,57,59,63,67,69,73,74,77,78,82,86",
                            "20,32,36,58,87" } };

            String[] strSubA;
            int[][] intScoreA = { { 0, 1 }, { 1, 0 } };
            // 标准分的全部内容
            String strStandard = "";

            // 按标准分得到“指导意见”
            String[] vDoctorIdeaA = new String[4];
            // 实际为4个因子
            float[][] vFactorA = new float[4][2];
            for (i = 0; i < vFactorA.length; i++) {
                for (j = 0; j < vFactorA[1].length; j++) {
                    vFactorA[i][j] = 0;
                }
            }

            // “原始分”
            for (i = 0; i < strNumberA.length; i++) {
                for (j = 0; j < strNumberA[1].length; j++) {
                    if (strNumberA[i][j].length() > 0) {
                        strSubA = strNumberA[i][j].split(",");
                        for (k = 0; k < strSubA.length; k++) {
                            vFactorA[j][0] += intScoreA[i][Integer.valueOf(sAnswer[Integer.valueOf(strSubA[k]) - 1])
                                    - 1];
                        }
                    }
                }
            }

            // “标准分”内容
            int intSex = answer.getSex();
            float floAge = answer.getAge();
            if (intSex == 1) {
                if (floAge <= 19) {
                    strStandard = "115,24,22,25,21,110,24,22,25,21,110,24,22,25,21,105,24,22,25,21,"
                            + "100,24,22,25,21,100,24,22,25,21,95,24,22,25,21,90,23,22,25,21,85,22,22,25,21,85,21,22,25,21,"
                            + "80,20,22,25,21,80,19,22,25,21,75,18,21,24,21,75,17,21,23,21,70,16,20,22,20,70,15,19,21,19,"
                            + "70,14,19,20,19,65,13,18,19,18,65,12,17,18,17,60,11,16,17,16,60,10,15,16,15,55,9,14,15,14,"
                            + "55,8,13,14,13,55,8,13,13,13,50,7,12,12,12,50,6,11,11,11,45,5,10,10,10,45,4,10,10,10,"
                            + "45,3,9,9,9,40,2,8,8,8,40,1,7,7,7,40,1,7,6,7,35,0,6,5,6,35,0,5,4,5,30,0,4,3,4,"
                            + "30,0,3,2,3,30,0,3,2,2,25,0,2,1,1,25,0,1,1,1,20,0,0,0,0,15,0,0,0,0";
                } else if (19 < floAge && floAge <= 29) {
                    strStandard = "115,24,22,25,22,110,23,22,25,22,110,22,22,25,22,105,21,22,25,22,"
                            + "100,20,22,25,22,100,19,22,25,22,95,18,22,25,22,90,17,22,25,22,85,16,22,25,22,85,15,22,25,22,"
                            + "80,14,22,24,22,80,14,22,23,22,75,13,21,22,21,75,12,21,21,20,70,11,20,20,19,70,11,19,19,18,"
                            + "70,11,18,19,18,65,10,17,18,17,65,10,16,17,17,60,9,15,16,16,60,8,14,15,15,55,7,13,14,14,"
                            + "55,7,12,13,13,50,6,11,12,12,50,5,10,11,11,45,4,9,10,10,45,4,7,9,10,40,3,6,8,9,"
                            + "40,2,5,7,8,35,1,4,6,7,35,1,3,5,6,30,0,2,4,5,30,0,1,3,4,25,0,0,2,3,25,0,0,1,2,20,0,0,0,1,15,0,0,0,0";
                } else if (29 < floAge && floAge <= 39) {
                    strStandard = "110,24,22,25,22,105,23,22,25,22,100,22,22,25,22,95,21,22,25,22,"
                            + "95,20,22,25,22,90,19,22,25,22,90,18,22,25,22,85,17,22,25,22,80,16,21,25,22,80,15,21,25,22,"
                            + "75,14,20,24,21,75,13,19,23,21,75,13,19,22,21,70,12,18,21,20,70,12,17,20,19,65,11,16,19,18,"
                            + "65,10,15,18,17,60,9,14,17,16,60,8,13,16,15,60,8,13,15,15,55,7,12,14,14,55,7,11,13,13,"
                            + "50,6,10,12,12,50,5,9,11,11,45,4,8,10,10,45,3,7,9,9,40,2,6,8,8,40,2,5,7,8,40,2,5,6,8,"
                            + "35,1,4,5,7,35,1,3,4,6,30,0,2,3,5,30,0,1,2,4,25,0,0,1,3,25,0,0,1,2,20,0,0,0,1,15,0,0,0,0";
                } else if (39 < floAge && floAge <= 49) {
                    strStandard = "120,23,22,25,22,115,22,22,25,22,110,21,22,25,22,110,20,22,25,22,"
                            + "105,19,22,25,22,100,18,22,25,22,95,17,22,25,22,90,16,22,25,22,90,15,22,25,22,85,14,22,25,22,"
                            + "80,13,22,24,22,75,12,21,23,22,75,11,20,22,22,70,10,19,21,21,70,10,18,20,20,70,10,18,19,20,"
                            + "65,9,17,18,19,65,9,16,17,18,65,9,16,16,18,60,8,15,15,17,60,8,14,14,16,60,8,13,14,16,"
                            + "55,7,12,13,15,55,6,11,12,14,55,6,11,11,14,50,5,10,10,13,50,5,9,9,13,45,4,8,8,12,"
                            + "45,4,7,7,11,45,4,6,6,11,40,3,5,5,10,40,3,4,4,9,35,2,3,3,8,35,1,2,2,7,35,1,1,1,7,"
                            + "30,0,0,0,6,25,0,0,0,5,25,0,0,0,4,20,0,0,0,3,20,0,0,0,2,15,0,0,0,1,10,0,0,0,0,5,0,0,0,0,0,0,0,0,0";
                } else if (49 < floAge && floAge <= 59) {
                    strStandard = "120,23,22,25,22,115,22,22,25,22,110,21,22,25,22,110,20,22,25,22,"
                            + "105,19,22,25,22,100,18,22,25,22,95,17,22,25,22,90,16,22,25,22,90,15,22,25,22,85,14,21,25,22,"
                            + "80,13,20,25,22,80,13,19,25,22,75,12,18,24,22,75,11,17,23,22,70,10,16,22,21,70,10,15,21,20,"
                            + "70,10,15,20,20,65,9,14,19,19,65,9,13,18,18,60,8,12,17,17,60,8,11,16,17,60,8,11,15,17,"
                            + "55,7,10,14,16,55,6,10,13,15,55,6,10,12,15,50,5,9,11,14,50,5,8,10,13,45,4,7,9,12,"
                            + "45,4,6,8,11,45,4,6,7,11,40,3,5,6,10,40,2,4,5,9,40,2,4,4,9,35,1,3,3,8,35,1,2,2,7,30,0,1,1,6,"
                            + "30,0,1,1,5,25,0,0,0,4,20,0,0,0,3,20,0,0,0,2,15,0,0,0,1,10,0,0,0,0";
                } else {
                    strStandard = "120,20,22,25,22,115,19,22,25,22,110,18,22,25,22,105,17,22,25,22,"
                            + "100,16,22,25,22,95,15,22,25,22,95,14,22,25,22,90,13,22,25,22,85,12,22,25,22,80,11,22,24,22,"
                            + "80,11,22,23,22,80,11,22,22,22,75,10,21,21,21,75,10,20,20,21,75,10,20,19,21,70,9,19,18,20,"
                            + "70,9,18,17,20,65,8,17,16,19,65,7,16,15,19,60,6,15,14,18,60,6,14,13,17,60,6,13,12,17,"
                            + "55,5,12,11,16,55,5,11,10,16,50,4,10,9,15,50,4,9,8,15,45,3,8,7,14,45,3,7,6,13,45,3,6,6,13,"
                            + "40,2,5,5,12,40,2,4,4,12,40,2,4,3,12,35,1,3,2,11,35,1,2,1,11,35,1,1,1,11,30,0,0,0,10,"
                            + "30,0,0,0,9,25,0,0,0,8,20,0,0,0,7,15,0,0,0,6,15,0,0,0,5,10,0,0,0,4,5,0,0,0,3,5,0,0,0,2,0,0,0,0,1";
                }
            } else {
                if (floAge <= 19) {
                    strStandard = "120,23,22,25,21,115,22,22,25,21,110,21,22,25,21,105,20,22,25,21,"
                            + "105,19,22,25,21,100,18,22,25,21,95,17,22,25,21,90,16,22,25,21,90,15,22,25,21,85,14,22,25,21,"
                            + "80,13,21,25,21,80,12,21,25,21,75,11,20,24,21,75,11,19,23,21,70,10,18,22,20,70,10,17,21,20,"
                            + "65,9,16,20,19,65,8,15,19,18,65,8,15,18,18,60,7,14,17,17,60,7,13,16,16,55,6,12,15,15,"
                            + "55,6,11,14,14,50,5,10,13,13,50,4,9,12,12,50,4,9,11,12,45,3,8,10,11,45,3,7,9,10,"
                            + "40,2,6,8,9,40,2,5,7,8,40,2,5,6,8,35,1,4,5,7,35,1,3,4,6,30,0,2,3,5,30,0,1,2,4,"
                            + "25,0,0,1,3,25,0,0,1,2,20,0,0,0,1,15,0,0,0,0";
                } else if (19 < floAge && floAge <= 29) {
                    strStandard = "115,23,22,25,23,110,22,22,25,23,105,21,22,25,23,105,20,22,25,23,"
                            + "100,19,22,25,23,95,18,22,25,23,90,17,22,25,23,90,16,22,25,23,85,15,22,25,23,80,14,21,25,23,"
                            + "80,13,21,25,23,75,12,20,24,22,75,12,19,23,22,70,11,18,22,21,70,10,17,21,20,65,9,16,20,19,"
                            + "65,9,15,19,18,65,9,14,19,18,60,8,13,18,17,60,7,12,17,16,60,7,12,16,16,55,6,11,14,15,"
                            + "55,6,10,14,14,50,5,9,13,13,50,4,8,12,12,45,3,7,11,11,45,3,6,10,11,45,3,5,10,11,"
                            + "40,2,4,9,10,40,1,3,8,9,35,0,2,7,8,35,0,1,6,7,35,0,1,5,7,30,0,0,4,6,30,0,0,3,5,"
                            + "25,0,0,2,4,25,0,0,1,3,20,0,0,0,2,15,0,0,0,1,10,0,0,0,0";
                } else if (29 < floAge && floAge <= 39) {
                    strStandard = "110,24,22,25,21,105,23,22,25,21,105,22,22,25,21,100,21,22,25,21,"
                            + "95,20,22,25,21,95,19,22,25,21,90,18,22,25,21,90,17,22,25,21,85,16,22,25,21,80,15,21,25,21,"
                            + "80,14,21,25,21,75,13,20,24,21,75,12,19,23,21,70,11,18,22,21,70,11,17,21,21,65,10,16,20,20,"
                            + "65,9,15,19,19,65,9,15,18,19,60,8,14,17,18,60,7,13,16,17,60,7,12,16,17,55,6,11,15,16,"
                            + "55,6,10,14,15,55,6,10,13,15,50,5,9,12,14,50,4,8,11,13,45,3,7,10,12,45,2,6,9,11,"
                            + "45,2,6,8,11,40,1,5,7,10,40,1,4,6,10,40,1,3,6,10,35,0,2,5,9,35,0,1,4,8,35,0,1,3,8,30,0,0,2,7,"
                            + "30,0,0,1,6,25,0,0,0,5,25,0,0,0,4,20,0,0,0,3,20,0,0,0,2,15,0,0,0,1,10,0,0,0,0";
                } else if (39 < floAge && floAge <= 49) {
                    strStandard = "120,20,22,25,21,115,19,22,25,21,110,18,22,25,21,105,17,22,25,21,"
                            + "100,16,22,25,21,100,15,22,25,21,95,14,22,25,21,90,13,22,25,21,85,12,22,25,21,80,11,21,25,21,"
                            + "80,11,20,25,21,75,10,19,25,21,75,9,18,25,21,70,8,17,24,21,70,8,16,23,21,70,8,16,22,21,"
                            + "65,7,15,21,20,65,7,14,20,19,65,7,14,19,19,60,6,13,18,18,60,6,12,17,18,55,5,11,16,17,"
                            + "55,5,10,15,16,55,5,10,14,16,50,4,9,13,15,50,3,8,12,14,50,3,7,11,14,45,2,6,10,13,"
                            + "45,2,5,9,13,45,2,5,8,13,40,1,4,7,12,40,1,3,6,11,40,1,3,5,11,35,0,2,4,10,35,0,1,3,10,35,0,1,2,10,"
                            + "30,0,0,1,9,30,0,0,1,8,25,0,0,0,7,25,0,0,0,6,20,0,0,0,5,15,0,0,0,4,15,0,0,0,3,10,0,0,0,2,5,0,0,0,1,0,0,0,0,0";
                } else if (49 < floAge && floAge <= 59) {
                    strStandard = "120,23,22,25,21,115,22,22,25,21,110,21,22,25,21,105,20,22,25,21,"
                            + "105,19,22,25,21,100,18,22,25,21,95,17,22,25,21,95,16,22,25,21,90,15,22,25,21,85,14,22,25,21,"
                            + "80,13,21,25,21,80,12,21,25,21,75,11,20,24,21,75,11,19,23,21,70,10,18,22,21,70,9,17,21,21,"
                            + "70,9,17,20,21,65,8,16,19,20,65,8,15,18,19,60,7,14,17,18,60,6,13,16,17,60,6,12,15,17,"
                            + "55,5,11,14,16,55,5,10,13,15,55,5,10,12,15,50,4,9,11,14,50,3,8,10,13,45,2,7,9,12,"
                            + "45,2,6,7,11,40,1,5,6,10,40,1,4,5,9,35,0,3,4,8,35,0,2,3,7,35,0,2,2,7,30,0,1,1,6,30,0,1,1,5,"
                            + "25,0,0,0,4,25,0,0,0,3,20,0,0,0,2,20,0,0,0,1,15,0,0,0,0";
                } else {
                    strStandard = "120,20,22,25,22,115,19,22,25,22,110,18,22,25,22,105,17,22,25,22,"
                            + "100,16,22,25,22,100,15,22,25,22,95,14,22,25,22,90,13,22,25,22,85,12,22,25,22,80,11,21,24,22,"
                            + "75,10,20,23,22,75,9,19,23,22,70,8,18,22,22,70,8,17,21,22,70,8,17,20,22,65,7,16,19,22,"
                            + "65,7,15,18,21,60,6,14,17,20,60,6,13,16,19,60,6,13,15,19,55,5,12,14,18,55,4,11,13,17,"
                            + "55,4,10,13,17,50,3,9,12,16,50,3,8,11,15,50,3,8,10,15,45,2,7,9,14,45,2,6,8,13,40,1,5,7,12,"
                            + "40,1,4,6,11,40,1,4,5,11,35,0,3,4,10,35,0,2,3,10,30,0,1,2,9,30,0,1,1,8,25,0,0,0,7,"
                            + "25,0,0,0,6,20,0,0,0,5,20,0,0,0,4,15,0,0,0,3,15,0,0,0,2,10,0,0,0,1,5,0,0,0,0,0,0,0,0,0";
                }
            }

            // “标准分”
            strSubA = strStandard.split(",");
            for (i = 0; i < 4; i++) {
                for (j = 0; j < strSubA.length + 1; j += 5) {
                    if (vFactorA[i][0] >= Integer.valueOf(strSubA[j + i + 1])) {
                        vFactorA[i][1] = Integer.valueOf(strSubA[j]);
                        break;
                    }
                }
            }

            // 按标准分得到“指导意见”
            float[] sngValueA = { 61.5f, 56.7f, 43.3f, 38.6f };
            for (i = 0; i < vFactorA.length; i++) {
                vDoctorIdeaA[i] = "Epq." + String.valueOf(i + 1) + ".5";
                for (j = 0; j < sngValueA.length; j++) {
                    if (vFactorA[i][1] >= sngValueA[j]) {
                        vDoctorIdeaA[i] = "Epq." + String.valueOf(i + 1) + "." + String.valueOf(j + 1);
                        break;
                    }
                }
            }

            ClsScore.Epq epq = new ClsScore.Epq();
            epq.setJsz_o(vFactorA[0][0]);
            epq.setJsz_s(vFactorA[0][1]);
            epq.setJsz_d(vDoctorIdeaA[0]);

            epq.setNwx_o(vFactorA[1][0]);
            epq.setNwx_s(vFactorA[1][1]);
            epq.setNwx_d(vDoctorIdeaA[1]);

            epq.setSjz_o(vFactorA[2][0]);
            epq.setSjz_s(vFactorA[2][1]);
            epq.setSjz_d(vDoctorIdeaA[2]);

            epq.setYscd_o(vFactorA[3][0]);
            epq.setYscd_s(vFactorA[3][1]);
            epq.setYscd_d(vDoctorIdeaA[3]);

            result.setObject(epq);
            return result;
        } catch (ArithmeticException e) {
            result.setMessage(e.toString());
            return result;
        }
    }


    private String[] GetEpqReport(Map map) {
        // 下面因子名称字段改成.toLowerCase()小写，是因为JAVA转json的时候会把大写字母转成小写字母
        // 第一个，错误信息；第二个，量表得分部分；第三个，量表指导意见_d数组部分
        // String[] strResult = {"","",""};
        String[] strResult = { "", "", "", "", "" };
        try {
            int i = 0;
            String[] gcName = "精神质,内外向,神经质,掩饰程度".split(",");
            String[] gdName = "P,E,N,L".split(",");
            String[] geName = "Jsz,Nwx,Sjz,Yscd".toLowerCase().split(",");

            String[] gvName = {};
            switch (g_CorE) {
                case 1:
                    gvName = gcName;
                    break;
                case 2:
                    gvName = gdName;
                    break;
            }

            if (g_inblFactor) // 因子得分
            {
                strResult[1] += "<table cellpadding='0' cellspacing='0' width='100%' border='0'>";

                strResult[1] += "<tr><td" + subTitle(0, tWidth) + ">指标</td><td" + subTitle(1, tWidth * 2)
                        + ">原始分</td><td" + subTitle(1, -1) + ">标准分</td></tr>";

                for (i = 0; i < gcName.length; i++) {
                    strResult[1] += "<tr><td" + subTitle(0, -1) + ">" + gvName[i] + "</td>" + "<td" + subValue(1, -1)
                            + ">" + map.get(geName[i] + "_o") + "</td>" + "<td" + subValue(1, -1) + ">"
                            + map.get(geName[i] + "_s");
                    if (i % 2 == 0) {
                        if (Float.valueOf(String.valueOf((map.get(geName[i] + "_s")))) >= 61.5) {
                            strResult[1] += red_Asterisk;
                        }
                    }
                    strResult[1] += "</td></tr>";
                }
                strResult[1] += "</table>";
            }

            String strCode = "";
            for (i = 0; i < gcName.length; i++) {
                strCode += map.get(geName[i] + "_d") + "|";
            }

            if (strCode.length() > 0) {
                strCode = strCode.substring(0, strCode.length() - 1);
            }

            strResult[2] = strCode;

            // 预警
            for (i = 0; i < gcName.length; i++) {
                if (i % 2 == 0) {
                    if (Float.valueOf(String.valueOf((map.get(geName[i] + "_s")))) >= 61.5) {
                        strResult[3] = "*";
                        strResult[4] += gcName[i].toString() + "异常，";
                    }
                }
            }

            if (strResult[4].length() > 0) {
                strResult[4] = strResult[4].substring(0, strResult[4].length() - 1);
            } else {
                strResult[3] = "0";
                strResult[4] = "正常";
            }

            return strResult;
        } catch (ArithmeticException e) {
            strResult[0] = "失败";
            return strResult;
        }
    }

}
